#!/bin/bash
cd ~
# create dir "Clipboard_trans"
if [ ! -d "Clipboard_trans"  ];then
  mkdir "Clipboard_trans"
fi
cd "Clipboard_trans"
mark=0

# tiff
result=$(osascript -e 'clipboard info' | grep '«class PNGf»')
if [[ "$result" != "" ]]
then
	echo -e "1.save tiff"
	osascript -e "get the clipboard as «class PNGf»" | sed "s/«data PNGf//; s/»//" | xxd -r -p > x.tiff

	echo "2.convert to png"
	sips -s format png -s formatOptions default x.tiff --out x.png

	echo "3.copy png to clipboard"
	osascript -e 'set the clipboard to (read "x.png" as «class PNGf»)'
	
	# exit shell
	exit
else
	echo "Clipboard not found tiff image."
fi

# pdf
result=$(osascript -e 'clipboard info' | grep '«class PDF »')
if [[ "$result" != "" ]]
then
	echo -e "1.save pdf"
	osascript -e "get the clipboard as «class PDF »" | sed "s/«data PDF //; s/»//" | xxd -r -p > x.pdf

	echo "2.convert to png"
	sips -s format png -s formatOptions default x.pdf --out x.png

	echo "3.copy png to clipboard"
	osascript -e 'set the clipboard to (read "x.png" as «class PNGf»)'

	# exit shell
	exit
else
	echo "Clipboard not found pdf image."
fi


echo "Nothing to do!!!"